﻿using System;
using System.Data.SQLite;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(int Description, int ReservedValue);
        //数据库连接
        SQLiteConnection m_dbConnection;
        public Form1()
        {
            InitializeComponent();
           // createNewDatabase();
            connectToDatabase();
           // createTable();
          //  fillTable();
            printHighscores();
        }

        //创建一个空的数据库
        void createNewDatabase()
        {
            SQLiteConnection.CreateFile("MyDatabase.db");
        }

        //创建一个连接到指定数据库
        void connectToDatabase()
        {
            m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.db;Version=3;");
            m_dbConnection.Open();
        }

        //在指定数据库中创建一个table
        void createTable()
        {
            string sql = "CREATE TABLE `activity_sign` ("+
  "`id` bigint(20)," +
  "`ac_id` bigint(20)," +
  "`reg_channel` tinyint(1)," +
  "`open_id` varchar(50)," +
  "`wechat_name` varchar(50)," +
  "`user_pin` varchar(100)," +
  "`u_name` varchar(100)," +
 "`phone` varchar(11)," +
  "`sex` char(1) ," +
  "`age` tinyint(4)," +
  "`birth_date` varchar(10)," +
  "`post_address` varchar(255)," +
  "`reg_flag` tinyint(1) ," +
  "`app_version` varchar(50)," +
  "`os_version` varchar(50)," +
 " `ip` varchar(50)," +
  "`other` varchar(255)," +
  "`modified` timestamp," +
  "`created` datetime," +
  "`yn` tinyint(1))";
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();
        }

        //插入一些数据
        void fillTable()
        {
            string sql = "INSERT INTO `activity_sign` (`id`, `ac_id`, `reg_channel`, `open_id`, `wechat_name`, `user_pin`, `u_name`, `phone`, `sex`, `age`, `birth_date`, `post_address`, `reg_flag`, `app_version`, `os_version`, `ip`, `other`, `modified`, `created`, `yn`) VALUES ('1', '1', '1', '1', 'test', 'test', 'test', '13031164430', 'm', '22', '89-07-30', '123', '0', '1', '1', '1', '1', '2018-12-18 13:58:41', '2018-12-18 14:00:03', '1');";
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();

        }

        //使用sql查询语句，并显示结果
        void printHighscores()
        {
            string sql = "select * from sign_user where open_id = '100001'";
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            SQLiteDataReader reader = command.ExecuteReader();
            while (reader.Read())
                Console.WriteLine("id: " + reader["id"] + "\twechat_name: " + reader["wechat_name"]);
            Console.ReadLine();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            /*string server = "www.baidu.com";
            Ping p = new Ping();
            PingReply pr;
            pr = p.Send(server);
            if (pr.Status != IPStatus.Success)
            {
                Console.WriteLine("未联网");
            }
            else
            {
                Console.WriteLine("已联网");
            }
            */
            if (checkNetwork())
            {
                toolStripStatusLabelNetwork.Text = "网络状态：正常";
            }
            else
            {
                toolStripStatusLabelNetwork.Text = "网络状态：已断网";
            }
        }
        //判断网络状态
       private bool checkNetwork()
        {
            if (InternetGetConnectedState(0, 0))
            {
                return true;
            }
            return false;
        }

        private void checkNetworkButton_Click(object sender, EventArgs e)
        {
            if (checkNetwork())
            {
                toolStripStatusLabelNetwork.Text = "网络状态：正常";
            }
            else
            {
                toolStripStatusLabelNetwork.Text = "网络状态：已断网";
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void toolStripComboBox1_Click(object sender, EventArgs e)
        {

        }
    }
}
